using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Threading;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Messaging;

namespace SpiderService
{
	public class SpiderThread : ThreadBase
	{
		private AutoResetEvent signal;
		public SpiderThread()
		{
			signal = new AutoResetEvent(true);
		}

		public void Set()
		{
			signal.Set();
		}

		protected override void DoWork()
		{
			while (this.started)
			{
				try
				{
					MessageQueue queue = Globals.LinkItemMQ.MessageQueue;
					queue.ReceiveCompleted += new ReceiveCompletedEventHandler(QueueReceiveCompleted);
					queue.BeginReceive();

					signal.WaitOne();
				}
				catch (Exception ex)
				{
					Globals.FileLog.WriteError(ex);
				}
				Thread.Sleep(Globals.SpiderThreadSleepTime);
			}
		}

		protected override void OnStop()
		{
			Set();
		}

		private void QueueReceiveCompleted(Object objSource, ReceiveCompletedEventArgs aSyncResult)
		{
			try
			{
				MessageQueue mq = (MessageQueue)objSource;
				using (mq)
				{
					if (mq.CanRead)
					{
						Message msg = mq.EndReceive(aSyncResult.AsyncResult);
						msg.Formatter = new BinaryMessageFormatter();

						WebFileInfo wfi = (WebFileInfo)msg.Body;
						if (wfi != null)
						{
							string html = GetWeb(wfi);
							if (html != null)
							{
								WebDataInfo gdi = new WebDataInfo();
								gdi.ProjectId = wfi.ProjectId;
								gdi.Url = wfi.Url;
								gdi.Data = html;

								Globals.DataItemMQ.Send(gdi);
							}
						}
						msg.Dispose();
					}
				}
			}
			catch (Exception ex)
			{
				Globals.FileLog.WriteError(ex);
			}
			finally
			{
				Set();
			}
		}

		public static string GetWeb(WebFileInfo gfi)
		{
			string html = null;
			HttpWebResponse response = null;
			StreamReader stream = null;
			try
			{
				SpiderProject project = ConfigDatas.GeProject(gfi.ProjectId);

				HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(gfi.Url);
				webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
				webRequest.KeepAlive = true;

				if (!string.IsNullOrEmpty(project.AcceptLanguage))
				{
					webRequest.Headers.Add("Accept-Language", project.AcceptLanguage);
				}

				if (!string.IsNullOrEmpty(project.Cookies))
				{
					webRequest.Headers.Add("Cookie", project.Cookies);
				}

				webRequest.ReadWriteTimeout = 30 * 1000;
				response = (HttpWebResponse)webRequest.GetResponse();
				if (response.StatusCode == HttpStatusCode.OK)
				{
					stream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(project.Encoding));
					html = stream.ReadToEnd();
				}
			}
			catch (Exception ex)
			{
				Globals.FileLog.WriteError(ex);
			}
			finally
			{
				if (response != null)
					response.Close();

				if (stream != null)
					stream.Close();
			}
			return html;
		}

		public class MyBinder : SerializationBinder
		{
			public override Type BindToType(string assemblyName, string typeName)
			{
				return Type.GetType(typeName);
			}
		}
	}

	
}
